package controller;

import com.mongodb.reactivestreams.client.MongoClients;
import dao.mongo.UserRepository;
import entity.mongo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import service.MongoService;

import java.util.List;
import java.util.concurrent.CountDownLatch;

@RestController
public class MongoController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private MongoService mongoService;

private static final Logger log = LoggerFactory.getLogger(MongoController.class);

    @RequestMapping(value = "countUser")
    public Mono<Long> countUser(@RequestBody User user) {
//        Mono<Long> longMono = testRepository.count();
        Mono<Long> longMono = userRepository.countByUsernameLike(user);
        return longMono;
    }

    @PostMapping("addUserList")
    public Flux<User> addUserList(@RequestBody List<User> list) {
        Flux<User> userFlux = mongoService.addUser(list);
        return userFlux;
    }

    @RequestMapping(value = "addUser")
    public Mono<User> addUser(@RequestBody User user) {
        if (user == null || user.getUsername() == null) {
            return null;
        }
        return userRepository.save(user);
    }

    @RequestMapping(value = "mongo")
    public User mongoAdd() {
        new User();
//        User user = new User("1", "123");
//        Mono<User> save = testRepository.save(user);
        try {
            this.insert();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        return user;
        return null;

    }
    CountDownLatch latch = new CountDownLatch(1);
    private User insert()  throws Exception{

        CountDownLatch latch = new CountDownLatch(1);

        ReactiveMongoTemplate mongoOps = new ReactiveMongoTemplate(MongoClients.create(), "flux");

//        mongoOps.insert(new User("Joe", "34"))
//                .flatMap(p -> mongoOps.findOne(new Query(where("name").is("Joe")), User.class))
//                .doOnNext(person -> log.info(person.toString()))
//                .flatMap(person -> mongoOps.dropCollection("person"))
////                .doOnComplete(latch::countDown)
//                .subscribe();

        latch.await();
//        ReactiveMongoTemplate mongoOps = new ReactiveMongoTemplate(MongoClients.create("mongodb://106.15.238.84:27017/"), "flux");
//
//        User user = new User("Joe", "34");
//        mongoOps.insert(user)
//                .flatMap(p -> mongoOps.findOne(new Query(where("name").is("Joe")), User.class))
//                .doOnNext(person -> log.info(person.toString()))
//                .flatMap(person -> mongoOps.dropCollection("person"))
//                .doOnComplete(latch::countDown)
//                .subscribe();
//
////        latch.await();
//        return user;
//        return new User("", "");
        return null;
    }
}
